/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cestum.cave.ui.components.wave
{
import mx.containers.HBox;
import mx.controls.Alert;
import mx.controls.Image;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.IFactory;
import mx.core.UIComponent;
import mx.events.FlexEvent;

import org.waveprotocol.wave.model.wave.data.WaveletData;

import spark.components.Label;


public class WaveItemRenderer extends UIComponent implements IFactory, IListItemRenderer, IDropInListItemRenderer{
	// IListItemRenderer implementation
	private var _data:Object;
	
	public function get data():Object{
		return _data;
	}
	
	public function set data(d:Object):void{
		_data = d;
		invalidateProperties();
		dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));

	}
	
	//IDropInListItemRenderer implementation:
	private var _listData:BaseListData;
	
	// Make the listData property bindable.
	[Bindable("dataChange")]
	
	public function get listData():BaseListData{
		return _listData;
	}
	
	public function set listData(value:BaseListData):void{
		_listData = value;
	}
	
	private var txt:Label;
	private var image:Image;
	private var hBox:HBox;
	//create your children here
	override protected function createChildren():void{
		super.createChildren();
		hBox = new HBox();
		txt = new Label();
		image = new Image();
		hBox.addChild(image);
		hBox.addChild(txt);
		hBox.addEventListener(FlexEvent.DATA_CHANGE, onChange);
		addChild(hBox);
	}
	
	
	override protected function commitProperties():void
	{
		super.commitProperties();
		//txt.styleName = getStyle('myawesometextstyleName');
		hBox.horizontalScrollPolicy = 'off';  
		hBox.verticalScrollPolicy = 'off';  
		hBox.percentWidth = 100; 
		hBox.percentHeight = 100; 
		if (this.data != null)
			txt.text = (WaveletData(this._data)).getWaveletName().waveletId.getId();
		image.source = "images/male_user.png";
		image.maintainAspectRatio = true;
		image.height = 24;
		image.width = 24;	
		image.verticalCenter = 0;
		image.left = 2;
		image.verticalA
		txt.setStyle("fontSize",12);
		txt.setStyle("fontWeight","bold");
		txt.verticalCenter = 2;
		invalidateSize();

  }
	
	override protected function measure():void {
		super.measure();
		measuredWidth = 100;
		measuredHeight = 30;
	}
	
	override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
		super.updateDisplayList(unscaledWidth, unscaledHeight );
		hBox.move(0,0);  
		hBox.setActualSize( (unscaledWidth-4), unscaledHeight);  
	}
	
	public function newInstance():*{
		return new WaveItemRenderer();
	}
	
	function onChange(event:FlexEvent):void {
		Alert.show("sss");	
	}
	
	
}

}